package com.daguchuangyi.erp_android.login;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.util.PreferenceUtil;
import com.jude.utils.JUtils;

/**
 * Created by lijianjian on 2018/3/3.
 */

public class IPInputDialog extends DialogFragment implements View.OnClickListener {

    private EditText mEditText;
    private TextView mCancelBtn;
    private TextView mConfirmBtn;

    private OnIpSelectListener mOnIpSelectListener;

    public void setOnIpSelectListener(OnIpSelectListener onIpSelectListener) {
        mOnIpSelectListener = onIpSelectListener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_ip_input_dialog, container, false);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mEditText = (EditText) view.findViewById(R.id.ip_input_edit);

        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (end > start) {
                    String destTxt = dest.toString();
                    String resultingTxt = destTxt.substring(0, dstart) +
                            source.subSequence(start, end) +
                            destTxt.substring(dend);
                    if (!resultingTxt.matches("^\\d{1,3}(\\." +
                            "(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
                        return "";
                    } else {
                        String[] splits = resultingTxt.split("\\.");
                        for (int i = 0; i < splits.length; i++) {
                            if (Integer.valueOf(splits[i]) > 255) {
                                return "";
                            }
                        }
                    }
                }
                return null;
            }
        };

//        mEditText.setFilters(filters);

        mCancelBtn = (TextView) view.findViewById(R.id.cancel_btn);
        mConfirmBtn = (TextView) view.findViewById(R.id.confirm_btn);
        mCancelBtn.setOnClickListener(this);
        mConfirmBtn.setOnClickListener(this);
        String savedIp = PreferenceUtil.getString(PreferenceUtil.TAG_IP_SAVED, "");
        if (!TextUtils.isEmpty(savedIp)) {
            mEditText.setText(savedIp);
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    @Override
    public void onDestroyView() {
        mOnIpSelectListener = null;
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_btn:
                if (mOnIpSelectListener != null) {
                    mOnIpSelectListener.onInputCancel();
                }
                dismiss();
                break;
            case R.id.confirm_btn:
                String ip_input = mEditText.getText().toString().trim();
                if (ip_input.startsWith("http://")) {
                    JUtils.Toast("不需要输入http协议前缀，请手动删除后重试");
                    return;
                }

                if (ip_input.contains("/")) {
                    JUtils.Toast("连接中不能包含\"/\",请检查后重试");
                    return;
                }

                if (Patterns.WEB_URL.matcher(ip_input).matches()) {
                    if (mOnIpSelectListener != null) {
                        mOnIpSelectListener.onIpInput(ip_input);
                    }
                    dismiss();
                } else {
                    JUtils.Toast("输入的地址不合法，请检查后重试");
                }
                break;
        }
    }

    public interface OnIpSelectListener {
        public void onIpInput(String ipAddress);

        public void onInputCancel();
    }

}
